package com.cy.dda.web.admin.rule;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.dda.model.PageBean;
import com.cy.dda.model.Result;
import com.cy.dda.model.ao.rule.AuthorizationAO;
import com.cy.dda.model.ao.rule.ManagerAddAO;
import com.cy.dda.model.ao.rule.ManagerEditAO;
import com.cy.dda.model.ao.rule.ManagerUpdatePasswordAO;
import com.cy.dda.model.ao.rule.RoleAddAO;
import com.cy.dda.model.ao.rule.RoleEditAO;
import com.cy.dda.model.domain.rule.Manager;
import com.cy.dda.model.domain.rule.Role;
import com.cy.dda.model.enums.CommonStatus;
import com.cy.dda.model.query.rule.ManagerListAdminQuery;
import com.cy.dda.model.query.rule.RoleListAdminQuery;
import com.cy.dda.model.vo.rule.ManagerListAdminVO;
import com.cy.dda.model.vo.rule.MenuTreeVO;
import com.cy.dda.model.vo.rule.MenuVO;
import com.cy.dda.service.rule.ManagerService;
import com.cy.dda.service.rule.RoleService;
import com.cy.dda.service.rule.RuleService;
import com.cy.dda.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 后台-权限管理
 *
 * @author: tanyilin
 * @create: 2018年11月9日 上午9:50:52
 */
@Slf4j
@RestController
@RequestMapping("/admin/rule")
@Api(tags = "后台-权限管理")
public class AdminRuleController extends BaseController {

	@Autowired
	private ManagerService managerService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private RuleService ruleService;
	
	@ApiOperation("角色列表，分页")
    @GetMapping("/role/list")
	public PageBean<Role> roleList(RoleListAdminQuery query) {
		PageBean<Role> pageBean = roleService.selectRoleListAdmin(query);
		return pageBean;
	}
	
	@ApiOperation("所有角色列表，不分页")
    @GetMapping("/all/roles")
	public Result<List<Role>> allRolesInCache() {
		List<Role> list = roleService.allRolesInCache();
		return ok("", list);
	}
	
	@ApiOperation("新增角色")
    @PostMapping("/role/add")
	public Result roleAdd(@Validated RoleAddAO roleAddAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        Role role = new Role();
        BeanUtils.copyProperties(roleAddAO, role);
        roleService.addRole(role);

        return ok();
    }
	
	@ApiOperation("编辑角色")
    @PostMapping("/role/edit")
    public Result roleEdit(@Validated RoleEditAO roleEditAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        Role role = new Role();
        BeanUtils.copyProperties(roleEditAO, role);
        roleService.updateRole(role);

        return ok();
    }
	
	@ApiOperation("管理员列表")
    @GetMapping("/manager/list")
    public PageBean<ManagerListAdminVO> managerList(ManagerListAdminQuery query) {
    	PageBean<ManagerListAdminVO> pageBean = managerService.selectManagerListAdmin(query);
        return pageBean;
    }
	
	@ApiOperation("添加管理员")
    @PostMapping("/manager/add")
    public Result managerAdd(@Validated ManagerAddAO managerAddAO, BindingResult bindingResult) {
        this.checkError(bindingResult);
        Manager manager = new Manager();
        BeanUtils.copyProperties(managerAddAO, manager);
        managerService.addManger(manager);
        return ok();
    }
	
	@ApiOperation("编辑管理员")
    @PostMapping("/manager/edit")
    public Result managerEdit(@Validated ManagerEditAO managerEditAO, BindingResult bindingResult) {
        this.checkError(bindingResult);
        Manager manager = new Manager();
        BeanUtils.copyProperties(managerEditAO, manager);
        managerService.updateManager(manager);
        return ok();
    }
	
	@ApiOperation("管理员更改账户状态")
    @PostMapping("/manager/update/status")
    public Result managerUpdateStatus(@RequestParam Long managerId, @RequestParam CommonStatus status) {
        managerService.updateStatus(managerId, status);
        return ok();
    }
	
	@ApiOperation("管理员修改密码")
    @PostMapping("/manager/update/pwd")
    public Result managerUpdatePwd(@Validated ManagerUpdatePasswordAO ao, BindingResult bindingResult) {
    	this.checkError(bindingResult);
    	managerService.updatePassword(this.getManager().getManagerId(), ao.getOldPassword(), ao.getNewPassword());
    	return ok();
    }
	
	@ApiOperation("管理员重置密码")
    @PostMapping("/manager/reset/pwd")
    public Result resetManagerPwd(@RequestParam Long managerId) {
    	managerService.resetManagerPwd(managerId);
    	return ok();
    }
	
	@ApiOperation("后台权限菜单树状图")
    @GetMapping("/tree")
    public Result<List<MenuTreeVO>> tree(Long roleId) {

        List<MenuTreeVO> tree = ruleService.selectMenuTreeInCache(roleId);

        return ok(null, tree);
    }
	
	@ApiOperation("给角色添加权限")
    @PostMapping("/authorization")
    public Result authorization(@Validated AuthorizationAO authorizationAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        ruleService.doAuthorization(authorizationAO.getRoleId(), authorizationAO.getMenuIds());

        return ok();
    }
	
}
